package cn.b.sky.user.controller;

import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.model.NumberRule;
import cn.b.sky.user.model.condition.NumberRuleCondition;
import cn.b.sky.user.service.NumberRuleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
@RequestMapping(value = "/numberRule/*")
public class NumberRuleController extends SecurityController {
  @Resource
  NumberRuleService numberRuleService;

  @RequestMapping("numberRuleList")
  public String numberRuleList(Model model, @ModelAttribute("condition") NumberRuleCondition number, String navTabId) {
    number.setSiteCode(SecurityContextUtil.getSiteCode());
    if (StringUtils.isEmpty(number.getOrderField())) {
      number.setOrderField("createDateTime");
    }
    List<NumberRule> numbers = numberRuleService.selectNumberRuleList(number);
    int count = numberRuleService.selectNumberRuleCount(number);
    number.setData(numbers);
    number.setTotalCount(count);
    model.addAttribute("page", number);
    model.addAttribute(NAVTABID, navTabId);
    return "cm/numberRule/numberRule_list";
  }
  
  @RequestMapping("numberRuleLookUp")
  public String numberRuleLookUp(Model model, @ModelAttribute("condition") NumberRuleCondition number, String navTabId) {
    number.setSiteCode(SecurityContextUtil.getSiteCode());
    if (StringUtils.isEmpty(number.getOrderField())) {
      number.setOrderField("createDateTime");
    }
    List<NumberRule> numbers = numberRuleService.selectNumberRuleList(number);
    int count = numberRuleService.selectNumberRuleCount(number);
    number.setData(numbers);
    number.setTotalCount(count);
    model.addAttribute("page", number);
    model.addAttribute(NAVTABID, navTabId);
    return "cm/numberRule/numberRule_lookup";
  }

  @RequestMapping("addNumberRule")
  public String addNumberRule(Model model, String navTabId) {
    model.addAttribute(NAVTABID, navTabId);
    return "cm/numberRule/numberRule_add";
  }

  @RequestMapping("editNumberRule")
  public String editNumberRule(Model model, String navTabId, String id) {
    model.addAttribute(NAVTABID, navTabId);
    NumberRule number = numberRuleService.selectNumberRule(SecurityContextUtil.getSiteCode(), id);
    model.addAttribute("number", number);
    return "cm/numberRule/numberRule_add";
  }

  @RequestMapping("saveNumberRule")
  @ResponseBody
  public DWZResult saveNumberRule(Model model, String navTabId, String id, NumberRule number, HttpServletRequest request) {
    if (number == null) {
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
    }
    number.setSiteCode(SecurityContextUtil.getSiteCode());
    setCreateInfo(number);
    if (StringUtils.isEmpty(id)) {
      numberRuleService.insertNumberRule(number);

      return dialogAjaxDone(getImeViewMessageI18n(null, "successfulOperation.M"), null, null, null, null);
    } else {
      numberRuleService.updateNumberRule(number);
      return dialogAjaxDone(getImeViewMessageI18n(null, "successfulOperation.M"), null, null, null, null);

    }

  }

  @RequestMapping("validateNumberRuleCode")
  @ResponseBody
  public boolean validateNumberRuleCode(String ruleCode, Model model) {
    if (!StringUtils.isBlank(ruleCode)) {
      return null == numberRuleService.selectNumberRule(SecurityContextUtil.getSiteCode(), ruleCode);
    }
    return true;
  }

  @RequestMapping("viewNumberRule")
  public String viewNumberRule(Model model, String navTabId, String id) {
    model.addAttribute(NAVTABID, navTabId);
    NumberRule number = numberRuleService.selectNumberRule(SecurityContextUtil.getSiteCode(), id);
    model.addAttribute("number", number);
    return "cm/numberRule/numberRule_view";
  }

  @RequestMapping("deleteNumberRule")
  @ResponseBody
  public DWZResult deleteNumberRule(Model model, String id, String navTabId, HttpServletRequest request) {
    numberRuleService.deleteNumberRule(SecurityContextUtil.getSiteCode(), id);
    return navTabAjaxDone(navTabId, null, null, null, getImeViewMessageI18n(null, "successfulOperation.M"), null);

  }

  @RequestMapping("/testRule")
  public void testRule(String id, HttpServletResponse response) throws IOException {
    PrintWriter writer = response.getWriter();
    String number = numberRuleService.selectNextNumber(SecurityContextUtil.getSiteCode(), id);
    writer.print(number);

  }

}
